<template>
    <div>
        <el-alert v-if="tis" :title="tis" type="success" show-icon :closable="false"/>
       <el-table :data="this.tableData" :row-key="getRowKeys"
       @selection-change="handleSelectionChange" ref="table" stripe>
        <tr v-for="(item, index) in this.tableLabel" :key="index">
            <el-table-column v-if="item.type==='checkbox'" fixed="left" type="selection" width="55" :selectable="checkCell" :reserve-selection="true"></el-table-column>
            <el-table-column v-else-if="item.type==='createTime'" :prop="item.prop" :label="item.label" :min-width="item.width" :formatter="formatDate"></el-table-column>
            <el-table-column v-else-if="item.type==='updateTime'" :prop="item.prop" :label="item.label" :min-width="item.width" :formatter="formatDate"></el-table-column>
            <el-table-column v-else-if="item.type==='statusFlag'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <template v-if="scope.row.statusFlag === '0'">
                  <el-tag type="warning">未发货</el-tag>
                </template>
                <template v-else-if="scope.row.statusFlag === '1'">
                    <el-tag type="success">已发货</el-tag>
                </template>
              </template>
            </el-table-column>
            <el-table-column v-else-if="item.type==='sendDeliverNum'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <template v-if="scope.row.statusFlag === '0'">
                 0
                </template>
                <template v-else-if="scope.row.statusFlag === '1'">
                  {{scope.row.deliverNum}}
                </template>
              </template>
            </el-table-column>
            
            <el-table-column v-else-if="item.type==='acceptFlag'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <template v-if="scope.row.acceptFlag === '0'">
                  <el-tag type="warning">待入库</el-tag>
                </template>
                <template v-else-if="scope.row.acceptFlag === '1'">
                    <el-tag type="success">已入库</el-tag>
                </template>
              </template>
            </el-table-column>
            <el-table-column v-else-if="item.type==='receivingDeliverNum'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <template v-if="scope.row.acceptFlag === '0'">
                 0
                </template>
                <template v-else-if="scope.row.acceptFlag === '1'">
                  {{scope.row.deliverNum}}
                </template>
              </template>
            </el-table-column>
            <el-table-column v-else-if="item.type==='remarks'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <p>
                  {{ scope.row.remarks | ellipsis}}
                  <el-button type="text" size="mini" @click="more(scope.row.remarks)" v-if="scope.row.remarks.length > 18">更多</el-button>
              </p>
              </template>
            </el-table-column>
            <el-table-column v-else-if="item.type==='putInRemarks'" :prop="item.prop" :label="item.label" :min-width="item.width">
              <template slot-scope="scope">
                <p>
                  {{ scope.row.putInRemarks | ellipsis}}
                  <el-button type="text" size="mini" @click="more(scope.row.putInRemarks)" v-if="scope.row.putInRemarks.length > 18">更多</el-button>
              </p>
              </template>
            </el-table-column>
            <el-table-column v-else-if="item.type==='normal'" :prop="item.prop" :label="item.label" :min-width="item.width"></el-table-column>
        </tr>
          <el-table-column label="操作" :fixed="this.models.fixed" :width="this.models.width">
              <template slot-scope="scope">
                  <template v-if="scope.row.statusFlag === '0'">
                    <el-button type="danger" v-if="scope.row.sendDeliver" size="mini" @click="sendDeliver(scope.row)">发货</el-button>
                  </template>
                  <el-button type="primary" v-if="scope.row.details" size="mini" @click="details(scope.row)">详情</el-button>
                  <template v-if="scope.row.acceptFlag === '0'">
                    <el-button type="danger" v-if="scope.row.warehousing" size="mini" @click="warehousing(scope.row)">入库</el-button>
                  </template>
              </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-pagination
            class="page-footer"
            background
            layout="total,prev,pager,next,sizes,jumper"
            :total="this.models.total"
            :page-sizes="[10,20,30,40,50]"
            @current-change="handlePageChange"
            @size-change="handleSizeChange"
          />
        </el-row>
        <el-dialog title="查看更多" :visible.sync="dialogVisible" width="30%">
          <div style="line-height: 22px;">{{contents}}</div>
        </el-dialog>
    </div>
  </template>
  
  <script>
import moment from 'moment'
  export default {
      props: {
          tableData: {
            type: Array,
            default: () => {
                return [];
            }
          },
          tableLabel:{
            type: Array,
            default: () => {
                return [];
            }
          },
          models: {
            type: Object,
            default: () => {
                return null;
            }
          },
      },
      data() {
          return {
            tis:"",
            ids:[],
            params: {
                type:"page",
                pageNum: 1,
                pageSize: 10
            },
            dialogVisible:false,
            contents:''
          };
      },
      filters: {
        ellipsis (value) {
            if (!value) return ''
            if (value.length > 18) {
                return value.slice(0,18) + '...'
            }
            return value
        }
      },
      mounted(){
        if(this.models.hackReset){
          this.$refs.table.clearSelection();
          this.ids = [];
          this.tis = "";
        }
      },
      methods: {
        checkCell(row){
          //已发货不能在批量发货
          return row.statusFlag === '0'
        },
        formatDate(row, column) {
            const date = row[column.property]
            if (date == '') {
            return ''
            }
            return moment(date).format("YYYY-MM-DD")
        },
        formatTime(row, column) {
            const date = row[column.property]
            if (date == '') {
            return ''
            }
            return moment(date).format("YYYY-MM-DD  HH:mm:ss")
        },
        more(content){
            this.contents = content;
            this.dialogVisible = true;
          },
        getRowKeys(row) {
            return row.id
        },
        handleSelectionChange(val) {
          let ids = [];
          val.forEach(item =>{ 
              ids.push(item)
          });
          this.ids = ids;
          this.tis="已选择 "+val.length+" 项";
          if(val.length==0){
            this.tis="";
            this.ids = [];
          }
          this.$emit("on-selection-click", this.ids);
        },
        sendDeliver(row){
              this.$emit("on-sendDeliver-click",row);
          },
          warehousing(row){
              this.$emit("on-warehousing-click",row);
          },
          details(row){
              this.$emit("on-details-click",row);
          },
          handlePageChange(page) {
             this.params.pageNum = page;
              this.loadList();
          },
          handleSizeChange(page) {
              this.params.pageSize = page;
              this.loadList();
          },
          loadList() {
              this.$emit("on-search-click", this.params);
          },
          onClose() {
              this.$emit("on-cancel");
          }
      }
  };
  </script>
  
  